package com.zycuse.nettyboot;

import com.zycuse.nettyboot.NettyServer.NettyServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
@Slf4j
@Controller
@RequestMapping("/request")
public class ServerRetransmissionControler {
    @Autowired
    NettyServerService nettyServerService;

    @RequestMapping(value = "/getPage", method = {RequestMethod.GET,RequestMethod.POST})
    public void getPage(@RequestParam(name = "url") String url, HttpServletResponse response) {
        log.info("url= " + url);
        String string = nettyServerService.sendRequest(url);

        OutputStream os = null;
        try {
            os = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Writer writer = new OutputStreamWriter(os);
        try {
            writer.write(string);
            writer.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
